﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import Engine.Managers.*;
    import flash.utils.*;
    import Engine.*;

	/**
	 * 所有地图上物品的基类，
	 * 维护一个正式ID和临时ID、以及一个事务计数和是否删除的标记 
	 */
    public class SavedObject {

        protected var m_id:Number = 0;
        protected var m_tempId:Number;
        protected var m_transactionCount:int = 0;
        protected var m_deleted:Boolean = false;

        public function SavedObject(){
            this.m_tempId = Constants.INDEX_NONE;
            super();
        }
        protected static function getInstance(_arg1:String, _arg2:String=null):SavedObject{
            var SavedObjectClassType:* = null;
            var instance:* = null;
            var className:* = _arg1;
            var itemName = _arg2;
            try {
                SavedObjectClassType = (getDefinitionByName(("Classes." + className)) as Class);
                if (itemName != null){
                    instance = new SavedObjectClassType(itemName);
                };
            } catch(e:ReferenceError) {
                ErrorManager.addError(("Type is not found " + className), ErrorManager.ERROR_REMOTEOBJECT_FAULT);
            };
            return (instance);
        }

        public function getId():Number{
            return (this.m_id);
        }
        public function getTempId():Number{
            return (this.m_tempId);
        }
        public function generateTempId():void{
            this.m_tempId = Utilities.generateUniqueId();
        }
        public function hasValidId():Boolean{
            var _local1:Boolean;
            if (this.m_id == 0){
                StatsManager.count("errors", "has_valid_id_is_zero");
                _local1 = false;
            };
            return (_local1);
        }
        public function setId(_arg1:Number):void{
            this.m_id = _arg1;
        }
        public function setTempId(_arg1:Number):void{
            this.m_tempId = _arg1;
        }
        public function hasId():Boolean{
            return (!((this.m_id == Constants.INDEX_NONE)));
        }
        public function getClassName():String{
            var _local1:Array = getQualifiedClassName(this).split("::");
            return (_local1[(_local1.length - 1)]);
        }
        public function markForDeletion(_arg1:Boolean):void{
            if (_arg1){
                this.m_deleted = true;
            } else {
                this.m_deleted = false;
            };
        }
        public function incrementTransactions():void{
            this.m_transactionCount++;
        }
        public function decrementTransactions():void{
            this.m_transactionCount--;
        }
        public function clearTransactions():void{
            this.m_transactionCount = 0;
        }
        public function needsToBeSaved():Boolean{
            return (((!((this.m_transactionCount == 0))) || (this.m_deleted)));
        }
        public function getSaveObject():Object{
            var _local1:Object = new Object();
            _local1.deleted = this.m_deleted;
            _local1.id = this.m_id;
            _local1.tempId = this.m_tempId;
            _local1.className = this.getClassName();
            return (_local1);
        }
        public function loadObject(_arg1:Object):void{
            if (_arg1 != null){
                this.m_deleted = _arg1.deleted;
                this.m_id = _arg1.id;
                this.m_tempId = _arg1.tempId;
            };
        }

    }
}//package Engine.Classes 
